package com.hax2max.control.externalclient;

import winterwell.jtwitter.*;
import winterwell.jtwitter.Twitter.Status;

import android.util.Log;

import com.hax2max.R;
import com.hax2max.control.FriendStalkerException;
import com.hax2max.model.ExternalData;


/**
 * Used for communicating with twitter
 * @author Peter
 *
 */

public class TwitterClient implements IExternalClient  {

	Twitter twitterHandle;
	boolean isTwitterUser;
	
	/**
	 * if using this constructor, user will not be able to set own tweets
	 */
	public TwitterClient()
	{
		twitterHandle = new Twitter();
		isTwitterUser = false;
	}
	
	/**
	 * This constructor will allow user to set own tweets (username and password have to be correct)
	 * @param userId
	 * @param password
	 */
	public TwitterClient(String userId, String password)
	{
		twitterHandle = new Twitter(userId, password);
		isTwitterUser = true;
	}
	
	/**
	 * loads tweet into ExternalData object. 
	 * the ed supplied have to contain a valid user.
	 * @param ed
	 * External data object containing a valid twitter userId
	 * 
	 * @returns
	 * External Data object containing a new tweet upon success, 
	 * otherwise a External Data object containing error message 
	 * and the ExternalData boolean success = false
	 */
	
	public ExternalData loadData(ExternalData ed) {
		
		try
		{
			Status twitterStatus = twitterHandle.getStatus(ed.userId);
			ed.success = true;
			ed.errorMsg = "";
			ed.statusText = twitterStatus.text;
			ed.latestUpdate = twitterStatus.createdAt;
			return ed;
		}
		catch(TwitterException e)
		{	
			Log.e("TwitterClient", " got exception when trying to get data (Status): " +e.getMessage());
			ed.success = false;
			ed.errorMsg = e.getMessage();
			return ed;
		}
	}

	/**
	 * First use the constructor (String userId, String password),
	 * then run this method to verify the user, (name + password)
	 * @returns 
	 * true user is valid
	 * false user is invalid or no user supplied in constructor
	 */
	public boolean testUserValid() {
		if(isTwitterUser)
			return twitterHandle.isValidLogin();
		else
			return false;
	}
	/**
	 * updates current users tweet
	 * @param ed
	 * ExternalData object containing the tweet to be committed to twitter
	 * @returns 
	 * true upon success
	 * false if no user is "logged in" or not successful
	 */
	public boolean updateStatus(ExternalData ed) 
	{
		if(!isTwitterUser) return false;
		try{
		String response = twitterHandle.setStatus(ed.statusText).text;
		if(response.equals(ed.statusText)){
				Log.i("TwitterClient", "set status, got "+ response + " back, perfect!");
				return true;
		}
		else
			Log.e("TwitterClient", "couldn't set status, got "+ response + " in response");
			return false;
		}
		catch(TwitterException ex)
		{
			Log.e("TwitterClient", "failed: exception: " + ex.getMessage());
			return false;
		}
	
	}

	/**
	 * not implemented
	 */
	public Object callOtherMethod(String method, String... parameter)
			throws FriendStalkerException.IExternalClient {
		throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientMethodNotImplemented);
		
	}
	
	/**
	 * returns a new TwitterClient if user/login is valid
	 */
	public IExternalClient changeUserAndLogin(String userId, String password)
			throws FriendStalkerException.IExternalClient {
		
		IExternalClient tc = new TwitterClient(userId, password);
		if(tc.testUserValid())
			return tc;
		else
			throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientCouldNotLogin);
	}
	

}
